package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.platform.entity.AppImage;
import com.vf.admin.platform.entity.Api;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ApiDao extends BaseDao<Api>{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     *保存或修改
     */
    public void save(Api client){
        if(client.getId()!=null&&client.getId()>0){
            this.update(client);
        }else{
            client.setId(this.add(client));
        }
    }

    /**
     *详情
     */
    public Api get(Integer id){
        if(id==null || StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_client  where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql=sbStr.toString();
        return this.queryObj(sql,paramList, Api.class);
    }

    /**
     *客户端接口列表
     */
    public List<Api> findList(Map<String,Object> paraMap, SplitPageBean splitPage){
        List<Object> paramList = new ArrayList<>();

        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from bt_client where 1=1");
        if(StringEx.isNotNull(paraMap.get("apiname"))){
            sbStr.append(" and apiname like ? ");
            paramList.add("%"+paraMap.get("apiname")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("apitype"))){
            sbStr.append(" and apitype = ? ");
            paramList.add(paraMap.get("apitype"));
        }
        if(StringEx.isNotNull(paraMap.get("loginstate"))){
            sbStr.append(" and loginstate = ?");
            paramList.add(paraMap.get("loginstate"));
        }

        String sql = sbStr.toString();

        return this.queryList(sql,paramList, Api.class,splitPage);
    }


    /**
     * 删除
     */
    public void del(int id){
        if(id<0){
            return ;
        }
        String sql = "delete  from bt_client  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 测试添加修改图片
     * @param image
     */
    public void saveImage(AppImage image){
        if(image.getId()!=null&&image.getId()>0){
            this.update(image);
        }else{
            image.setId(this.add(image));
        }
    }

    //测试图片对象
    public AppImage getImage(Integer id){
        if(id==null || StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_image  where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql=sbStr.toString();
        return this.queryObj(sql,paramList,AppImage.class);
    }

    //测试表集合
    public List<AppImage> findListImage(SplitPageBean splitPage){
        List<Object> paramList = new ArrayList<>();

        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_image where 1=1");
        String sql = sbStr.toString();

        return this.queryList(sql,paramList,AppImage.class,splitPage);
    }
}
